var loginurl = "./login.action";
var regediturl = "./regedit.action";
var oldpassword = "./oldpassword.action";
var manageUserInfo = "./manageUserInfo.action";
var updateUserInfo = "./updateUserInfo.action";
var deleteUinfo = "./deleteUinfo.action";
var saveQuest = "./saveQuest.action";

function submitDate(str) {
	$.ajax( {
		type : "POST",
		dataType : "text",
		url : './service',
		data : str,
		success : function(data) {
			alert(data);
		}
	});
}
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
function getValueById(id) {
	return document.getElementById(id).value;
}
function loginin() {
	var u_name = getValueById("u_name");
	var u_password = getValueById("u_password");

	if (u_name.trim() == '') {
		alert("用户名不能为空！");
		return;
	} else if (u_password.trim() == '') {
		alert("密码不能为空！");
		return;
	}
	var data = "u_name=" + codeEncode(u_name) + "&u_password=" + u_password;
	$("#loginDiv").fadeOut("fast",function(){
		window.location.href = loginurl + "?" + data;
	});
	// jQuery.post(loginurl, data);
}

function regedit() {
	var u_name = getValueById("u_name");
	var u_password = getValueById("u_password");
	var querenpassword = getValueById("querenpassword");
	
	if (u_name.trim() == '') {
		alert("用户名不能为空！");
		return;
	} else if (u_password.trim() == '') {
		alert("密码不能为空！");
		return;
	} else if (u_password.trim() != querenpassword.trim()) {
		alert("两次输入的密码不同！");
		return;
	}
	var data = "u_name=" + codeEncode(u_name) + "&u_password=" + u_password;
	// jQuery.post(regediturl, data);
	window.location.href = regediturl + "?" + data;
}

function addUserJsp() {
	window.location.href = "./addUserJsp.action";
}

function administerAddUser() {
	var u_name = getValueById("u_name");
	var u_password = getValueById("u_password");

	if (u_name.trim() == '') {
		alert("用户名不能为空！");
		return;
	} else if (u_password.trim() == '') {
		alert("密码不能为空！");
		return;
	}
	var data = "u_name=" + codeEncode(u_name) + "&u_password=" + u_password;
	$.ajax( {
		type : "POST",
		dataType : "text",
		url : './addUser.action',
		data : data,
		success : function(data) {
			alert(data);
		}
	});
}

function revamp() {
	var u_name = getValueById("u_name");
	var old_password = getValueById("old_password");
	var u_password = getValueById("u_password");
	var new_password = getValueById("new_password");

	if (u_name.trim() == '') {
		aler("用户名不能为空！");
		return;
	} else if (old_password.trim() == '') {
		alert("请输入旧密码！");
		return;
	} else if (u_password.trim() == '' || new_password.trim() == '') {
		alert("密码不能为空！");
		return;
	} else if (new_password.trim() != u_password.trim()) {
		alert("两次密码不同！");
		return;
	}

	var data = "u_name=" + codeEncode(u_name) + "&u_password=" + old_password
			+ "&new_password=" + new_password;
	$.ajax( {
		type : "POST",
		dataType : "text",
		url : oldpassword,
		data : data,
		success : function(data) {
			document.getElementById("old_password").value = data.split(",")[1];
			alert(data.split(",")[0]);
		}
	});

}

function searchUserByName() {
	var key = getValueById("key");
	window.location.href = manageUserInfo + "?key=" + codeEncode(key);
}

function updateInfo(uid) {
	window.location.href = updateUserInfo + "?uid=" + uid;
}

function deleteInfo(uid) {
	window.location.href = deleteUinfo + "?uid=" + uid;
}

function addUser() {
	window.location.href = "./regeditJsp.action";
}

function addQuestion() {
	window.location.href = "./questInfoJsp.action";
}

function searchQuestByKey() {
	window.location.href = "./manageQuest.action?key="
			+ codeEncode(getValueById("key"));
}

function updateQuest(q_id) {
	window.location.href = "./updateQuest.action?q_id=" + q_id;
}

function deleteQuest(q_id) {
	window.location.href = "./deleteQuest.action?q_id=" + q_id;
}

function questInfo(q_id) {
	window.location.href = "./questInfo.action?q_id=" + q_id;
}

function savequest() {
	var quest = getValueById("quest");
	var questA = getValueById("questA");
	var questB = getValueById("questB");
	var questC = getValueById("questC");
	var questD = getValueById("questD");
	var questresult = $("#result option:selected").html();
	var q_type = $("#q_type option:selected").attr("value");

	var data = "quest=" + codeEncode(quest) + "&questA=" + codeEncode(questA)
			+ "&questB=" + codeEncode(questB) + "&questC=" + codeEncode(questC)
			+ "&questD=" + codeEncode(questD) + "&questresult="
			+ codeEncode(questresult) + "&q_type=" + codeEncode(q_type);
	window.location.href = saveQuest + "?" + data;
}

function codeEncode(data) {
	return encodeURIComponent(encodeURIComponent(data));
}

function fenye(href) {
	window.location.href = href.replace("&key=null", "");
}

function updatequestinfoSubmit() {
	var quest = document.updatequestSubmit.quest.value.trim();
	var questA = document.updatequestSubmit.questA.value.trim();
	var questB = document.updatequestSubmit.questB.value.trim();
	var questC = document.updatequestSubmit.questC.value.trim();
	var questD = document.updatequestSubmit.questD.value.trim();
	if(quest == '' || quest == null ||
	   questA == '' || questA == null ||
	   questB == '' || questB == null ||
	   questC == '' || questC == null ||
	   questD == '' || questD == null )
	{	
		alert("信息不能有空，请检查！");
		return;
	}
	document.updatequestSubmit.submit();
}
/*
 * 动效
 */
function examSubmit() {
	if (confirm("确认交卷？")) {
		document.exam.submit();
	} else {

	}
}
